import { trackOrderPage, trackPass, trackReject } from '../services/service';
import React from 'react';
import { Input, message, Select } from 'antd';
const { Option } = Select;

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '项目简称',
    dataIndex: 'customName',
    ellipsis: true,
    fixed: 'left',
    width: '100px',
  },
  {
    title: 'load号',
    dataIndex: 'loadNo',
    ellipsis: true,
    fixed: 'left',
    width: '120px',
  },
  {
    title: '订单状态',
    dataIndex: 'status',
    ellipsis: true,
    fixed: 'left',
    width: '80px',
    render: text => {
      switch (text) {
        case 'ENTRY':
          return <span>生成</span>;
        case 'SPLIT':
          return <span>拆单</span>;
        case 'SPLITED':
          return <span>拆单完成</span>;
        case 'DISPATCH':
          return <span>调度</span>;
        case 'COST_CONFIRMATION':
          return <span>成本确认</span>;
        case 'COST_AUDIT':
          return <span>成本审核</span>;
        case 'ORDER_AUDIT':
          return <span>订单审核</span>;
        case 'LOAD_IN':
          return <span>货物装车</span>;
        case 'LOAD_OFF':
          return <span>货物送达</span>;
        case 'RECEIPT':
          return <span>收到回单</span>;
        case 'COMMON':
          return <span>承运</span>;
        case 'DELIVERY':
          return <span>送达</span>;
        case 'GATHERING':
          return <span>收款</span>;
        case 'GATHERING_AUDIT':
          return <span>收入审核</span>;
        case 'INVOICE':
          return <span>开票</span>;
        case 'FINISH':
          return <span>完成</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '提货日期',
    dataIndex: 'pickDate',
    align: 'center',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '到货日期',
    dataIndex: 'aogDate',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '业务类型',
    dataIndex: 'busType',
    width: '120px',
    ellipsis: true,
    render: text => {
      switch (text) {
        case 'LD':
          return <span>零担</span>;
        case 'DC':
          return <span>单程</span>;
        case 'WF':
          return <span>往返</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '要车类型',
    dataIndex: 'carType',
    ellipsis: true,
    width: '100px',
  },
  {
    title: '实际车型',
    dataIndex: 'realCarType',
    ellipsis: true,
    width: '100px',
  },
  {
    title: '装货地',
    dataIndex: 'loadingPlace',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '装货工厂',
    dataIndex: 'loadingFactory',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '卸货地',
    dataIndex: 'loadoffPlace',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '卸货工厂',
    dataIndex: 'loadoffFactory',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '司机',
    dataIndex: 'driver',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '车牌号',
    dataIndex: 'carNo',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '司机身份证',
    dataIndex: 'drIdcard',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '司机电话',
    dataIndex: 'drPhone',
    ellipsis: true,
    width: '120px',
  },
  {
    title: 'DN号',
    dataIndex: 'dnNo',
    ellipsis: true,
    width: '120px',
  },
  {
    title: '提货费',
    dataIndex: 'pickFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '干线费',
    dataIndex: 'arteryFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '送货费',
    dataIndex: 'dropoffFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '加点费',
    dataIndex: 'workoverFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '装货费',
    dataIndex: 'loadupFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '卸货费',
    dataIndex: 'loadoffFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '包车费',
    dataIndex: 'carFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '燃油补助费',
    dataIndex: 'fuelFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '加班费',
    dataIndex: 'callbackFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '押车费',
    dataIndex: 'escortinFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '小费',
    dataIndex: 'tipFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '短驳费',
    dataIndex: 'shuttleFeeOut',
    width: '120px',
    editable: false,
    align: 'center',
    ellipsis: true,
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: 'LOAD号',
        name: 'loadNo',
        component: <Input placeholder="请输入LOAD号" />,
      },
      {
        md: 6,
        label: 'DN号',
        name: 'dnNo',
        component: <Input placeholder="请输入DN号" />,
      },
      {
        md: 6,
        label: '司机姓名',
        name: 'driver',
        component: <Input placeholder="请输入司机姓名" />,
      },
      {
        md: 6,
        label: '车牌号',
        name: 'carNo',
        component: <Input placeholder="请输入车牌号" />,
      },
      {
        md: 6,
        label: '订单状态',
        name: 'status',
        component: (
          <Select placeholder="请输入订单状态">
            <Option value="COST_AUDIT">成本审核</Option>
            <Option value="LOAD_IN">货物装车</Option>
            <Option value="LOAD_OFF">货物送达</Option>
            <Option value="RECEIPT">收到回单</Option>
          </Select>
        ),
      },
    ],
  },
];
export default {
  namespace: 'trackOrder',
  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(trackOrderPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *trackPass({ payload }, { call, put }) {
      const res = yield call(trackPass, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *trackReject({ payload }, { call, put }) {
      const res = yield call(trackReject, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
  },
  reducers: {
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },
    // 表格选中Id
    handleSelection(state, action) {
      const { selectedRowKeys, selectedRows } = action.payload;
      return {
        ...state,
        selectionIds: selectedRowKeys,
        selectedRows: selectedRows,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
          isDirty: false,
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理发送
    audit_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('操作成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '操作失败！');
      }
      return {
        ...state,
        selectionIds: [],
        selectedRows: [],
      };
    },
  },
};
